package svgedit.gui.actions;

import java.awt.event.ActionEvent;
import java.util.Iterator;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import javax.swing.AbstractAction;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JToggleButton;

import svgedit.gui.Frame;

/**
 *  To monitor when translate to German language
 * */
public class EnglishLanAction extends AbstractAction{
	private Frame frame;
	
	/** Resource Bundle for current user's locale */
    private ResourceBundle srcBundle;
	
    /** Create this action for the given frame */
    public EnglishLanAction(Frame frame) {
        super("English");
        this.frame = frame;
    }

    /** To translate all the text on each component
     * 
     * @param ae to monitor mouse event
     * */
    public void actionPerformed(ActionEvent ae) {
       
    	// Get the resource bundle and point it to German property
    	srcBundle = ResourceBundle.getBundle( 
                "English", 
                frame.getLocales()[1]);
    	Set set = frame.getCompos().entrySet();
    	Iterator it = set.iterator();
    	while (it.hasNext()){
    		Map.Entry me = (Map.Entry)it.next();
    		String type = (String)me.getKey();
    		if (type.startsWith("menu")){
    			JMenu menu = (JMenu)me.getValue();
    			String str = menu.getText().trim().replaceAll(" ", "");
    			menu.setText(srcBundle.getString(str));
    			for (int i=0; i < menu.getItemCount(); i++){
    				JMenuItem menuItem = (JMenuItem)menu.getItem(i);
    				String string = menuItem.getText().trim().replaceAll(" ", "");
        			menuItem.setText(srcBundle.getString(string));
    			}
    		}
    		else if (type.startsWith("button")){
    			JToggleButton button = (JToggleButton)me.getValue();
    			String str = button.getText().trim().replaceAll(" ", "");
    			button.setText(srcBundle.getString(str));
    		}
    		else if (type.startsWith("label")){
    			JLabel label = (JLabel)me.getValue();
    			String string = label.getText().trim().replaceAll(" ", "");
    			label.setText(srcBundle.getString(string));
    		}
    		else if (type.startsWith("optionPane")){
    			JOptionPane optionPane = (JOptionPane)me.getValue();
    			String message = (String)optionPane.getMessage();
    			System.out.println(message);
    			String string = message.trim().replaceAll(" ", "");
    			optionPane.setMessage((String)srcBundle.getString(string));
    		}
    	}
    	
    }
}
